//
//  UIAlertControllerUtil.m
//  YiJuZhangShou2.0
//
//  Created by 冷悦 on 16/4/5.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "UIAlertControllerUtil.h"

@implementation UIAlertControllerUtil

+ (void)generateInfoControllerWithTitle:(NSString *)title
                       message:(NSString *)message{
    UIViewController *viewController = [UIAlertControllerUtil getCurrentViewController];
    DDLogInfo(@"viewController is %@", viewController);
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title
                                                                             message:message
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:resetAction];
    [viewController presentViewController:alertController animated:YES completion:nil];
}

+ (UIViewController *)getCurrentViewController{
    UIViewController* activityViewController = nil;
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if(window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *tmpWin in windows){
            if(tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    
    NSArray *viewsArray = [window subviews];
    
    if([viewsArray count] > 0)
    {
        UIView *frontView = [viewsArray objectAtIndex:0];
        
        id nextResponder = [frontView nextResponder];
        
        if([nextResponder isKindOfClass:[UIViewController class]])
        {
            activityViewController = nextResponder;
        }
        else
        {
            activityViewController = window.rootViewController;
        }
    }
    
    UIViewController *returnViewController = activityViewController.presentedViewController == nil ? activityViewController : activityViewController.presentedViewController;
    
    return returnViewController;
}

@end
